<template>
	<view class="container">
		<view class="category-nav">
			<view 
				v-for="(item, index) in categoryList" 
				:key="index"
				:class="['category-item', currentCategoryIndex === index ? 'active' : '']"
				@click="handleCategoryClick(index)"
			>
				{{ item }}
			</view>
		</view>
		<view class="product-list">
			<view 
				class="product-item" 
				v-for="(item, idx) in productList" 
				:key="item.proid"
				@click="goToDetail(item.proid)">
				<image :src="item.img2" mode="widthFix" class="product-img"></image>
				<view class="product-name">{{ item.proname }}</view>
				<view class="product-desc">分类：{{ item.category }} | 品牌：{{ item.brand }}</view>
				<view class="product-price">¥{{ item.originprice }}</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			categoryList: [],
			productList: [],
			currentCategoryIndex: 0,
			pageNum: 1,
			limitNum: 20
		};
	},
	onLoad() {
		this.loadCategories();
		this.loadProducts();
	},
	methods: {
		loadCategories() {
			uni.request({
				url: 'http://47.94.6.186:3001/api/pro/categorylist',
				method: 'GET',
				data: { category: '示例分类值' },
				success: res => {
					console.log('分类数据:', res.data);
					this.categoryList = res.data.data || [];
				},
			});
		},
		loadProducts() {
			uni.request({
				url: 'http://47.94.6.186:3001/api/pro/list',
				method: 'GET',
				data: { 
					count: this.pageNum, 
					limitNum: this.limitNum
				},
				success: res => {
					console.log('商品数据:', res.data);
					this.productList = res.data.data || [];
				},
			});
		},
		handleCategoryClick(index) {
			this.currentCategoryIndex = index;
		},

		goToDetail(proid) {
			uni.navigateTo({
				url: `/pages/detail/detail?proid=${proid}`
			});
		}
	},
};
</script>

<style scoped>
	
.container {
	display: flex;
	height: 100vh;
}
.category-nav {
	width: 20%;
	max-width: 100px;
	background: #f8f8f8;
	overflow-y: auto;
	border-right: 1px solid #eee;
}
.category-item {
	padding: 16px 10px;
	text-align: center;
	font-size: 14px;
	border-bottom: 1px solid #eee;
}
.category-item.active {
	background-color: #fff;
	color: #ff6700;
	font-weight: bold;
}
.product-list {
	flex: 1;
	overflow-y: auto;
	display: flex;
	flex-wrap: wrap;
}
.product-item {
	padding: 10rpx;
	width: calc(45%);
	background: #fff;
	border-radius: 6px;
	box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.product-img {
	width: 100%;
	border-radius: 4px;
}
.product-name {
	font-size: 14px;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	margin: 5px 0;
}
.product-desc {
	font-size: 12px;
	color: #666;
	margin-bottom: 5px;
}
.product-price {
	font-size: 14px;
	color: #ff6700;
}
</style>

